package com.zhao.fishingguideserver.controller;

import com.zhao.fishingguideserver.POJO.Product;
import com.zhao.fishingguideserver.POJO.Result;
import com.zhao.fishingguideserver.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端商品管理控制器
 */
@RestController
@RequestMapping("/admin/product")
public class AdminProductController {
    
    @Autowired
    private ProductMapper productMapper;
    
    /**
     * 获取商品列表
     */
    @GetMapping("/list")
    public Result<List<Product>> getProductList(@RequestParam(required = false) String keyword,
                                                 @RequestParam(required = false) String status) {
        try {
            List<Product> products;
            if (status != null && !status.isEmpty()) {
                // 根据状态筛选
                products = productMapper.selectAll().stream()
                    .filter(p -> status.equals(p.getStatus()))
                    .toList();
            } else {
                products = productMapper.selectAll();
            }
            return Result.success(products);
        } catch (Exception e) {
            return Result.error("获取商品列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取商品详情
     */
    @GetMapping("/detail/{id}")
    public Result<Product> getProductDetail(@PathVariable Long id) {
        try {
            Product product = productMapper.selectByPrimaryKey(id);
            if (product == null) {
                return Result.error("商品不存在");
            }
            return Result.success(product);
        } catch (Exception e) {
            return Result.error("获取商品详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新商品信息
     */
    @PutMapping("/update")
    public Result<Void> updateProduct(@RequestBody Product product) {
        try {
            int result = productMapper.updateByPrimaryKeySelective(product);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("更新商品失败");
            }
        } catch (Exception e) {
            return Result.error("更新商品失败：" + e.getMessage());
        }
    }
    
    /**
     * 修改商品状态
     */
    @PutMapping("/status/{id}")
    public Result<Void> updateProductStatus(@PathVariable Long id,
                                            @RequestParam String status) {
        try {
            Product product = new Product();
            product.setProductId(id);
            product.setStatus(status);
            int result = productMapper.updateByPrimaryKeySelective(product);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("更新商品状态失败");
            }
        } catch (Exception e) {
            return Result.error("更新商品状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除商品
     */
    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteProduct(@PathVariable Long id) {
        try {
            int result = productMapper.deleteByPrimaryKey(id);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("删除商品失败");
            }
        } catch (Exception e) {
            return Result.error("删除商品失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除商品
     */
    @PostMapping("/batchDelete")
    public Result<Void> batchDeleteProducts(@RequestBody List<Long> ids) {
        try {
            int successCount = 0;
            for (Long id : ids) {
                int result = productMapper.deleteByPrimaryKey(id);
                if (result > 0) {
                    successCount++;
                }
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }
}

